package cn.shiwei.org.controller;

import cn.shiwei.basic.util.PageList;
import cn.shiwei.basic.util.RequestStatus;
import cn.shiwei.org.domain.Employee;
import cn.shiwei.org.query.EmployeeQuery;
import cn.shiwei.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/employee")
@Api(tags = "员工接口",description = "员工接口详细描述")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    @GetMapping("/manage")
    @ApiOperation(value = "查询部门管理者", notes = "不需要参数")
    private List<Employee> getEmployeeList(){
        return employeeService.loadAll();
    }

    /**
     * 分页查询
     * @RequestBody : 前端的对象数据是封装到了请求体中, 从请求体中取数据用 @RequestBody注解
     * @param query
     * @return
     */
    @PostMapping
    @ApiOperation(value = "部门列表分页查询",notes = "需要传参 为高级查询的条件")
    private PageList<Employee> queryPage(@RequestBody EmployeeQuery query){
        System.out.println(query);
        return employeeService.queryDate(query);
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除删除",notes = "需要参数id")
    private RequestStatus delete(@PathVariable("id") Long id){
        try {
            employeeService.remove(id);
            return RequestStatus.me();
        } catch (Exception e) {
            e.printStackTrace();
            return RequestStatus.me().setMessage("删除失败");
        }
    }

    @PatchMapping
    @ApiOperation(value = "根据id批量删除",notes = "需要参数id")
    private RequestStatus patchDelete(@RequestBody List<Long> ids){
        try {
            System.out.println(ids);
            employeeService.patchRemove(ids);
            return RequestStatus.me();
        } catch (Exception e) {
            e.printStackTrace();
            return RequestStatus.me().setMessage("删除失败");
        }
    }

}
